"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
function CustomToString(target) {
    target.prototype.toString = function () {
        return JSON.stringify(this);
    };
    Object.seal(target.prototype); // 封印对象的原型链，以后不允许其他操作
}
let Person2 = class Person2 {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
};
Person2 = __decorate([
    CustomToString
], Person2);
const p11 = new Person2("John", 30);
console.log(p11.toString()); // {"name":"John","age":30}
//创建一个装饰器，为类添加日志功能和创建时间
function LogTime(target) {
    return class extends target {
        constructor(...args) {
            super(...args);
            this.createdTime = new Date();
        }
        getTime() {
            return this.createdTime;
        }
    };
}
let User = class User {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
    log() {
        console.log(`User ${this.name} is ${this.age} years old.`);
    }
};
User = __decorate([
    LogTime
], User);
function RangeValidate(min, max) {
    return function (target, propertyKey, descriptor) {
        // 保存原始的 setter 方法
        const originalSetter = descriptor.set;
        // 修改 setter 方法，在设置属性值之前进行校验
        descriptor.set = function (value) {
            if (value < min || value > max) {
                throw new Error(`Value must be between ${min} and ${max}.`);
            }
            // 如果值在范围内
            if (originalSetter) {
                originalSetter.call(this, value);
            }
        };
    };
}
class weather {
    constructor(_temp) {
        this._temp = _temp;
    }
    set temp(value) {
        this._temp = value;
    }
    get temp() {
        return this._temp;
    }
}
__decorate([
    RangeValidate(-50, 50)
], weather.prototype, "temp", null);
const w1 = new weather(30);
w1.temp = 40; // 正常
w1.temp = 100; // 抛出异常
